package com.greensam.pixelengine.config;

import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.core.config.Order;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 请求包装过滤器
 *
 * @author Ma Chengrui
 * @since 2025/9/28 16:19
 */
@Order(1)
@Component
public class HttpRequestWrapperFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        if (request instanceof HttpServletRequest servletRequest) {
            String contentType = servletRequest.getHeader(Header.CONTENT_TYPE.getValue());
            if (StringUtils.isNotBlank(contentType) && contentType.contains(ContentType.JSON.getValue())) {
                // 可以再细粒度一些，只有需要进行空间权限校验的接口才需要包一层
                chain.doFilter(new RequestWrapper(servletRequest), response);
            } else {
                chain.doFilter(request, response);
            }
        }
    }

}
